package com.zheng.draft.admin.controller.manage;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.zheng.draft.common.constant.DraftResult;
import com.zheng.draft.common.constant.DraftResultConstant;
import com.zheng.draft.dao.model.DraftInfo;
import com.zheng.draft.dao.model.DraftInfoExample;
import com.zheng.draft.rpc.api.DraftInfoService;
import com.zheng.common.base.BaseController;
import com.zheng.common.validator.LengthValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 票据控制器
 * Created by shuzheng on 2016/11/14.
 */
@Controller
@Api(value = "票据管理", description = "票据管理")
@RequestMapping("/manage/info")
public class DraftInfoController extends BaseController {

	private static Logger _log = LoggerFactory.getLogger(DraftInfoController.class);
	
	@Autowired
	private DraftInfoService draftInfoService;

	@ApiOperation(value = "票据首页")
	@RequiresPermissions("draft:info:read")
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index() {
		return "/manage/info/index.jsp";
	}

	@ApiOperation(value = "票据列表")
	@RequiresPermissions("draft:info:read")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Object list(
			@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
			@RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
			@RequestParam(required = false, value = "sort") String sort,
			@RequestParam(required = false, value = "order") String order,
			@RequestParam(required = false, value = "draftNo") String draftNo,
			@RequestParam(required = false, value = "draftName") String draftName) {
		DraftInfoExample draftInfoExample = new DraftInfoExample();
		if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
			draftInfoExample.setOrderByClause(sort + " " + order);
		}
		if(StringUtils.isNotEmpty(draftNo)){
			draftInfoExample.or().andDraftNoLike("%"+draftNo+"%");
		}
		if(StringUtils.isNotEmpty(draftName)){
			draftInfoExample.or().andDraftNameLike(draftName);
		}
		List<DraftInfo> rows = draftInfoService.selectByExampleForOffsetPage(draftInfoExample, offset, limit);
		long total = draftInfoService.countByExample(draftInfoExample);
		Map<String, Object> result = new HashMap<>();
		result.put("rows", rows);
		result.put("total", total);
		return result;
	}

	@ApiOperation(value = "新增票据")
	@RequiresPermissions("draft:info:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(ModelMap modelMap) {
		DraftInfoExample draftInfoExample = new DraftInfoExample();
		draftInfoExample.setOrderByClause("draft_no desc");
		List<DraftInfo> draftInfos = draftInfoService.selectByExample(draftInfoExample);
		modelMap.put("draftInfos", draftInfos);
		return "/manage/info/create.jsp";
	}

	@ApiOperation(value = "新增票据")
	@RequiresPermissions("draft:info:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public Object create(DraftInfo draftInfo) {
		ComplexResult result = FluentValidator.checkAll()
				.on(draftInfo.getDraftNo(), new LengthValidator(1, 50, "票据号"))
				.doValidate()
				.result(ResultCollectors.toComplex());
		if (!result.isSuccess()) {
			return new DraftResult(DraftResultConstant.INVALID_LENGTH, result.getErrors());
		}
		int count = draftInfoService.insertSelective(draftInfo);
		return new DraftResult(DraftResultConstant.SUCCESS, count);
	}

	@ApiOperation(value = "删除票据")
	@RequiresPermissions("draft:info:delete")
	@RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
	@ResponseBody
	public Object delete(@PathVariable("ids") String ids) {
		int count = draftInfoService.deleteByPrimaryKeys(ids);
		_log.info("删除票据成功！[{}]",ids);
		return new DraftResult(DraftResultConstant.SUCCESS, count);
	}

	@ApiOperation(value = "修改票据")
	@RequiresPermissions("draft:info:update")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable("id") int id, ModelMap modelMap) {
		DraftInfoExample draftInfoExample = new DraftInfoExample();
		draftInfoExample.setOrderByClause("draft_no desc");
		List<DraftInfo> draftInfos = draftInfoService.selectByExample(draftInfoExample);
		DraftInfo info = draftInfoService.selectByPrimaryKey(id);
		modelMap.put("draftInfos", draftInfos);
		modelMap.put("info", info);
		return "/manage/info/update.jsp";
	}

	@ApiOperation(value = "修改票据")
	@RequiresPermissions("draft:info:update")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
	@ResponseBody
	public Object update(@PathVariable("id") int id, DraftInfo draftInfo) {
		ComplexResult result = FluentValidator.checkAll()
				.on(draftInfo.getDraftNo(), new LengthValidator(1, 50, "票据号"))
				.doValidate()
				.result(ResultCollectors.toComplex());
		if (!result.isSuccess()) {
			return new DraftResult(DraftResultConstant.INVALID_LENGTH, result.getErrors());
		}
		draftInfo.setDraftId(id);
		int count = draftInfoService.updateByPrimaryKeySelective(draftInfo);
		return new DraftResult(DraftResultConstant.SUCCESS, count);
	}

}